/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.icanci.loopstack.damc.admin.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import cn.icanci.loopstack.damc.admin.biz.mapper.AppMapper;
import cn.icanci.loopstack.damc.admin.biz.service.AppService;
import cn.icanci.loopstack.damc.admin.common.model.TextValue;
import cn.icanci.loopstack.damc.admin.common.model.base.AppVO;
import cn.icanci.loopstack.damc.admin.dal.common.PageList;
import cn.icanci.loopstack.damc.admin.dal.mongo.daointerface.AppDAO;
import cn.icanci.loopstack.damc.admin.dal.mongo.dateobject.AppDO;

/**
 * @author icanci
 * @since 1.0 Created in 2025/05/16 17:26
 */
@Service("appService")
public class AppServiceImpl implements AppService {
    @Resource
    private AppDAO    appDAO;
    @Resource
    private AppMapper appMapper;

    /**
     * 查询所有 T
     *
     * @return 返回所有T
     */
    @Override
    public List<AppVO> queryAll() {
        return appMapper.dos2vos(appDAO.queryAll());
    }

    /**
     * 保存或者更新一条信息
     *
     * @param appVO t
     */
    @Override
    public void save(AppVO appVO) {
        // TODO 日志记录
        if (doInsert(appVO)) {
            AppDO insert = appMapper.vo2do(appVO);
            appDAO.insert(insert);
        } else {
            appDAO.update(appMapper.vo2do(appVO));
        }
    }

    /**
     * 根据id进行查询
     *
     * @param id id
     * @return 返回查询的结果
     */
    @Override
    public AppVO queryById(String id) {
        return appMapper.do2vo(appDAO.queryOneById(id));
    }

    /**
     * 分页查询
     *
     * @param appVO    T
     * @param pageNum  pageNum
     * @param pageSize pageSize
     * @return 返回分页查询结果
     */
    @Override
    public PageList<AppVO> queryPage(AppVO appVO, int pageNum, int pageSize) {
        PageList<AppDO> pageQuery = appDAO.pageQuery(appMapper.vo2do(appVO), pageNum, pageSize);
        return new PageList<>(appMapper.dos2vos(pageQuery.getData()), pageQuery.getPaginator());
    }

    /**
     * 获取 Selector
     *
     * @return TextValue
     */
    @Override
    public List<TextValue> loadAllSelector() {
        List<AppVO> apps = queryAll();
        List<TextValue> textValues = Lists.newArrayList();
        for (AppVO app : apps) {
            String label;
            if (isDeleted(app)) {
                label = String.format(DELETED_FORMAT, app.getAppName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, app.getAppName());
            }
            String value = app.getAppCode();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    /**
     * queryByName
     *
     * @param name name
     * @return AppDO
     */
    @Override
    public AppVO queryByName(String name) {
        return appMapper.do2vo(appDAO.queryByAppName(name));
    }

    /**
     * queryByCode
     *
     * @param code code
     * @return AppDO
     */
    @Override
    public AppVO queryByCode(String code) {
        return appMapper.do2vo(appDAO.queryByApCode(code));
    }
}
